package com.sansui.blog.controller.admin.api;

import com.sansui.blog.pojo.User;
import com.sansui.blog.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @name: UploadFileController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-07-24 22:18
 **/

//文件上传接口
@Controller
@RequestMapping("/admin")
public class UploadFileController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 1.文件保存在服务器，url地址保存在数据库
     * 上传成功之后返回成功保存的url地址
     */

    @PostMapping("/api/uploadAvatars")
    @ResponseBody
    public Map<String, Object> uploadAvatars(@RequestParam(value = "file") MultipartFile file, HttpSession session) throws IOException{
//        User user = (User) session.getAttribute("user");
        Map<String,Object> map = (Map<String, Object>) session.getAttribute("user");
        Map<String,Object> data = userInfoService.uploadAvatars(file,String.valueOf(map.get("uid")));
        return data;
    }


}
